Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

154
Vistas
Will jQuery or AJAX make HTML "required" attribute invalid?

I'm now constructing a registration page and have labeled username and password inputs as required. Simple code as:

Username <input id="username" type="text" required/>
Password <input id="username" type="password" required/>
<button id="register">Register</button>
<div id="registerOutput></div>

When I just run those code with pure HTML on localhost the "required" would function properly. But once I include a register.js file which I made to get the data and use AJAX to send data to register.php, the required attribute seems not working. In register.js, simple code as:

$(document).ready(function(){
    $("#register").on("click", function(){

        var username        = $("#username").val();
        var password        = $("#password").val();

        var userData = "username=" + username + "&password=" + password;

        $.ajax({
            method: "post",
            url: "register.php?",
            data: userData,
            success: function(backData){
                $("#registerOutput").html(backData);
            }
        });
    });
});

Can anybody tell me the reason why jQuery would override the functionality of required? How to surpass it or if it must override, how should I do the same thing in my jQuery code?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You might want to redesign your code a little bit. There is a way to follow HTML convenience and yet use full power of AJAX.

I edited your example in jsfiddle: https://jsfiddle.net/myezf63b/

So here is modified HTML:

<form id="form">
  Username <input id="username" type="text" required/>
  Password <input id="username" type="password" required/>
  <button type="submit" id="register">Register</button>
</form>

<div id="registerOutput">
</div>

Basically I added type="submit" to button and wrapped it inside form.

and this is modified jQuery code:

I added e.preventDefault() which prevents website from redirecting (reloading page) and as event I used submit which follows this pattern.

$(document).ready(function(){ $("#form").on("submit", function(e){
    e.preventDefault()
    var username        = $("#username").val();
    var password        = $("#password").val();

    var userData = "username=" + username + "&password=" + password;
        console.log('test')
    $.ajax({
        method: "post",
        url: "register.php?",
        data: userData,
        success: function(backData){
            $("#registerOutput").html(backData);
        }
    });
  });

});
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda